<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\user;



use App\Dao\MoneyDao;
use App\Dao\UserCashDao;
use App\Kernel\Util\UserJwtInstance;
use App\Middleware\User\UserMiddleware;

use App\Request\User\User\UserCashRequest;
use App\Service\User\MoneyService;
use App\Service\User\UserCashService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\Snowflake\IdGeneratorInterface;

/**
 * 提现控制器
 *
 * @AutoController()
 * @Middleware(UserMiddleware::class)
 * @author 张昊(2396675557@qq.com)
 * @package App\Controller\admin
 */
class UserCashController extends Controller
{
    /**
     * @Inject
     * @var UserCashDao
     */
    private $dao;

    /**
     * @Inject
     * @var UserCashService
     */
    private $service;

    /**
     * @Inject
     * @var MoneyService
     */
    public $money_service;

    /**
     * 提现记录
     */
    public function get()
    {
        $result = $this->dao->get();

        $this->success($result);
    }
    /**
     * 新增提现
     *
     * @param UserCashRequest $request
     */
    public function create(UserCashRequest $request)
    {
        $params = $request->all();

        $user = UserJwtInstance::instance()->build()->getUser();
        if( !empty($user->safe_pwd) ) {
            //验证安全密码
            if( !password_verify( $params['safe_pwd'], $user->safe_pwd ) ) {
                $this->error('ORDER.REGISTER_FAIL');
            }
        } else {
            $this->error('ORDER.REGISTER_EMPTY');
        }

        $user_money = $this->money_service->getMoneyBalanceByUser(uid());

        if( $user_money < $params['amount'] ) {
            $this->error("USER.NOT_MONEY");
        }

        if( $params['amount']  < 200 ) {
            $this->error("USER.NOT_TWO_HUNDRED");
        }

        $order_no = (string)$this->container->get(IdGeneratorInterface::class)->generate();

        $create = $this->dao->insertGetId([
            'user_id' => uid(),
            'amount' => $params['amount'],
            'order_no' => $order_no,
            'bank_name' => $params['bank_name'],
            'bank_account' => $params['bank_account'],
            'bank_realname' => $params['bank_realname'],
            'bank_idcard' => $params['bank_idcard'],
            'bank_phone' => $params['bank_phone'],
            'bank_type' => 1,
            'status' => $this->service::PENDING,
            'start_time' => time(),
            'created_at' => time(),
            'updated_at' => time(),
        ]);

        $this->money_service->money_dao->create([
            'status' => $this->money_service->money_dao::STATUS_ENABLE,
            'user_id' => uid(),
            'type' => $this->money_service->money_dao::CONSUME_TYPE,
            'source' => $this->money_service->money_dao::SOURCE_CASH,
            'price_source' => 2,
            'num' => $params['amount'],
            'created_at' => time(),
        ]);

        $this->money_service->updateMoneyUserBalance( uid() );

        !$create && $this->error();

        $this->success($create);
    }

    /**
     * 取消提现
     */
    public function delete()
    {
        $cash_id = (int)$this->request->input('cash_id', 0);
        if ($cash_id === 0) $this->error();

        $this->dao->delete($cash_id);

        $this->success();
    }

    /**
     * 通过提现ID 获取提现数据
     */
    public function getUserCashById()
    {
        $cash_id = (int)$this->request->input('cash_id', 0);

        $result = $this->dao->first($cash_id,['id','order_no','amount','bank_name','bank_account','start_time']);


        $this->success($result);

    }
}